<template>
  <div class="searchTop">
    <div class="icon" @click="$router.go(-1)">X</div>
    <input
      type="text"
      placeholder="周杰伦"
      v-model="searchKeyWord"
      @keydown.enter="enterSearch"
    />
  </div>
  <div class="searchHistory">
    <span class="searchSpan">历史</span>
    <span
      class="spanKey"
      v-for="(item, index) in keyWordList"
      :key="index"
      @click="historyShow(item)"
      >{{ item }}</span
    >
    <i class="icon" @click="deleteKey">X</i>
  </div>
      <div class="itemList">
      <div class="item" v-for="(item, index) in searchList" :key="index">
        <div class="itemLeft" @click="playSearch(item,index)">
          <span class="leftSpan">{{ index + 1 }}</span>
          <div>
            <p>{{ item.name }}</p>
            <span v-for="(Iname,i) in item.artists" :key="i">{{Iname.name}}</span>
          </div>
        </div>
        <div class="itemRight">
          <span  class="icon bofang">X</span>
          <span  class="icon liebiao">O</span>
        </div>
      </div>
    </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex';
import { searchMusic } from "../request/api/home";
export default {
  data() {
    return {
      keyWordList: [],
      searchKeyWord: "",
      searchList:[],
    };
  },
  computed:{
    ...mapState(['playlist'])
  },
  mounted() {
    this.keyWordList = JSON.parse(localStorage.getItem("keyWordList")) || [];
  },
  methods: {
    // 按下enter进行搜索
    async enterSearch() {
      //new Set()自动去重
      if (this.searchKeyWord !== "") {
        this.keyWordList.unshift(this.searchKeyWord);
        // 去重
        this.keyWordList = [...new Set(this.keyWordList)];
        // 固定长度
        if (this.keyWordList.length > 6) {
          this.keyWordList.splice(5, 1);
        }
        localStorage.setItem("keyWordList", JSON.stringify(this.keyWordList));
        console.log(this.searchKeyWord);
        let res = await searchMusic(this.searchKeyWord);
        // console.log(res);
        this.searchList = res.data.result.songs
        this.searchKeyWord = "";
      }
    },
    // 删除历史记录
    deleteKey() {
      this.keyWordList = [];
      localStorage.removeItem("keyWordList");
    },
    // 点击搜索记录实现搜索
    async historyShow(item) {
       let res = await searchMusic(item);
        console.log(res);
        this.searchList = res.data.result.songs
    },
    // 点击歌曲列表把歌曲添加到播放列表
    playSearch(item){
      item.al=item.album
      item.al.picUrl=item.album.artist.img1v1Url
      this.pushPlayList(item)
      this.updatePlayListIndex(this.playlist.length-1)
    },
    ...mapMutations(['pushPlayList','updatePlayListIndex'])
  },
};
</script>

<style lang="less" scoped>
.searchTop {
  width: 100%;
  height: 1rem;
  padding: 0 0.2rem;
  display: flex;
  align-items: center;
  input {
    margin-left: 0.2rem;
    border: none;
    border-bottom: 1px solid #999;
    width: 90%;
    padding: 0.1rem;
  }
}
.searchHistory {
  width: 100%;
  padding: 0.2rem;
  position: relative;
  .searchSpan {
    font-weight: 700;
  }
  .spanKey {
    padding: 0.1rem 0.2rem;
    background-color: rgb(185, 169, 169);
    border-radius: 0.4rem;
    margin: 0.1rem 0.2rem;
    display: inline-block;
  }
  .icon {
    width: 0.3rem;
    height: 0.3rem;
    position: absolute;
    right: 0.2rem;
  }
}
.itemList {
  width: 100%;
  padding: 0.2rem;
  .item {
    width: 100%;
    height: 1.4rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .itemLeft {
      width: 85%;
      height: 100%;
      display: flex;
      align-items: center;
      .leftSpan {
        display: inline-block;
        width: 0.2rem;
        text-align: center;
      }
      div {
        p {
          width: 4.54rem;
          height: 0.4rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-weight: 700;
        }
        span {
          font-weight: 400;
          font-size: 0.24rem;
          color: #999;
        }
        margin-left: 0.3rem;
      }
    }
    .itemRight {
      width: 20%;
      height: 100%;
      display: flex;
      // justify-content: space-between;
      align-items: center;
      position: relative;
      .icon {
        fill: #999;
      }
      .bofang {
        position: absolute;
        left: 0;
      }
      .liebiao {
        position: absolute;
        right: 0;
      }
    }
  }
}
</style>